// 导入vuex模块 
import { createStore } from 'vuex'
// 引入staff数据类型 
import Staff from '@/models/staff'



// 定义一个存储数据的结构 --- 实体类 
export interface MyState {
    collapse: boolean,  // 控制侧边栏的收缩
    staff: Staff, // 记录当前登录的员工
}

// 创建一个store对象 
const store = createStore<MyState>({

    // state --- 存储了具体的值
    state: {
        collapse: false,
        staff: {
            id: "", name:"", branch:0, area:0, roles:""
        }
    },

    // mutations --- 修改state中值的函数 
    mutations:{
        // 修改collapse的值
        setCollapse(state: MyState, collapse: boolean){
            state.collapse = collapse;
            
        },
        // 设置员工
        setStaff(state:MyState, staff: Staff){
            state.staff = staff;
            // 写入到localstorage中 
            localStorage.setItem('staff', JSON.stringify(staff))
           
        }
    },

    // getter --- 获取state值中的函数
    getters:{
        getCollapse(state:MyState){
            return state.collapse;
        },
        // 获取员工
        getStaff(state:MyState){
            // 从localstorage中获取 
            state.staff = JSON.parse(localStorage.getItem('staff'))
            return state.staff
        }
    }
})

// 暴露
export default store